#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;
using std::remove_if;
using std::for_each;

bool func(int value)
{
    return value > 5;
}
void test()
{
    vector<int>  vec = {1, 3, 6, 8, 7, 5, 6, 4, 3, 2};
    for_each(vec.begin(), vec.end(), 
             [](int &value){ cout << value << "  ";});
    cout << endl;

    //删除所有大于5的元素
    //remove_if不能将真正满足条件的元素删除，但是会返回待删除元素的首
    //迭代器，然后配合容器的erase操作，才能将真正的元素全部删除
    /* auto it = remove_if(vec.begin(), vec.end(), func); */
    /* auto it = remove_if(vec.begin(), vec.end(), */ 
    /*                     bind1st(std::less<int>(), 5)); */
    auto it = remove_if(vec.begin(), vec.end(), 
                        bind2nd(std::greater<int>(), 5));

    //删除所有小于5的元素
    /* auto it = remove_if(vec.begin(), vec.end(), */ 
    /*                     bind2nd(std::less<int>(), 5)); */

    /* auto it = remove_if(vec.begin(), vec.end(), [](int value)->bool{ */ 
    /*                     return value > 5; }); */
    vec.erase(it, vec.end());
    for_each(vec.begin(), vec.end(), 
             [](int &value){ cout << value << "  ";});
    cout << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

